package com.tongyu.bezier.view;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tongyu.bezier.R;

import java.util.Random;


/*
 *@Author TongYu
 *@Date: 2021/9/1
 *@Describe:
 */
public class ShopBezier extends RelativeLayout {
    int[] imgs;
    private int width;
    private int height;

    public ShopBezier(Context context) {
        super(context);
        init();
    }

    public ShopBezier(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        imgs = new int[]{R.drawable.red_dian};
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    public void addImageView() {
        ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(imgs[new Random().nextInt(imgs.length)]);
        addView(imageView);
        //设置动画
        CustomAnimator(imageView).start();
    }

    public ValueAnimator CustomAnimator(ImageView imageView) {
        Evaluator evaluator = new Evaluator(new PointF(width, height / 10));
        PointF startPointF = new PointF(width / 2, height / 2);
        PointF endPointF = new PointF(950, 1400);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(evaluator, startPointF, endPointF);
        valueAnimator.setDuration(3000);
        valueAnimator.setTarget(imageView);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });
        return valueAnimator;
    }

    //类型评估器
    class Evaluator implements TypeEvaluator<PointF> {
        PointF pointF1;

        public Evaluator(PointF pointF1) {
            this.pointF1 = pointF1;
        }

        @Override
        public PointF evaluate(float mTime, PointF startValue, PointF endValue) {
            float time = 1.0f - mTime;
            PointF pointF = new PointF();
            pointF.x = time * time * (startValue.x)
                    + 2 * time * mTime * (pointF1.x)
                    + mTime * mTime * (endValue.x);
            pointF.y = time * time * (startValue.y)
                    + 2 * time * mTime * (pointF1.y)
                    + mTime * mTime * (endValue.y);
            return pointF;
        }
    }
}
